package com.wangxiang.sys.manage.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangxiang.common.utils.*;
import com.wangxiang.sys.manage.dao.ShopAuditDao;
import com.wangxiang.sys.manage.entity.ShopAuditEntity;
import com.wangxiang.sys.manage.service.RoleUserService;
import com.wangxiang.sys.manage.service.ShopAuditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


@Service("shopAuditService")
public class ShopAuditServiceImpl extends ServiceImpl<ShopAuditDao, ShopAuditEntity> implements ShopAuditService {
    @Autowired
    private RoleUserService roleUserService;
    @Autowired
    private ShopAuditDao shopAuditDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ShopAuditEntity> page = this.page(
                new Query<ShopAuditEntity>().getPage(params),
                new QueryWrapper<ShopAuditEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 涉及多个表的查询
     * @param map
     * @return
     */
    @Override
    public ShopAuditEntity add(HashMap<String, String> map) {
//        System.out.println(map);

        HashMap<String, String> relEntity = roleUserService.getRelEntity(Integer.valueOf(map.get("roleId")));
        ShopAuditEntity shopAuditEntity = new ShopAuditEntity();
        shopAuditEntity.setShopId(Integer.valueOf(map.get("shopId")));
        shopAuditEntity.setShopDbName( map.get("shopDbName"));
        shopAuditEntity.setShopType( map.get("shopType"));
        shopAuditEntity.setShopName( map.get("shopName"));
        shopAuditEntity.setApplyName( relEntity.get("applyName"));
        shopAuditEntity.setApplyId(Integer.valueOf(relEntity.get("applyId")));
        shopAuditEntity.setApplyTime(new Date());
        shopAuditEntity.setAuditAuthName(relEntity.get("roleName"));


        int insert = shopAuditDao.insert(shopAuditEntity);
        if (insert>0){
            return shopAuditEntity;
        }
//        System.out.println(shopAuditEntity);
        return null;
    }

    @Override
    public PageEntity page(PageEntity pageEntity) {
        PageHelper.startPage(pageEntity.getPageNum(),pageEntity.getPageSize());
        List<ShopAuditEntity> shopAuditEntities = shopAuditDao.selectList(null);
        TimeUtils timeUtils = new TimeUtils();
        shopAuditEntities.forEach((item)->{
            item.setTimeString(timeUtils.timeChange(item.getApplyTime()));
        });
        PageInfo<ShopAuditEntity> shopAuditEntityPageInfo = new PageInfo<>(shopAuditEntities);
        PageGenerate pageGenerate = new PageGenerate();
        PageEntity generate = pageGenerate.generate(shopAuditEntityPageInfo);
        return generate;
    }

}